<!doctype html>
<html>
<head>
	<meta charset="utf-8">
	<link rel="stylesheet" type="text/css" href="__PUBLIC__/css/bootstrap.min.css">
	<link href="__PUBLIC__/dist/summernote.css" rel="stylesheet"/>
	<link href="__PUBLIC__/css/font-awesome.min.css?v=4.4.0" rel="stylesheet">
	<link href="__PUBLIC__/css/animate.min.css" rel="stylesheet">

</head>
<body>
<div class="summernote" id="summernote"></div>
<div align="right">
	<button type="button" id="post" class="btn btn-primary">
		<i class="fa fa-check"></i>
		发表
	</button>
</div>

</body>
<script src="__PUBLIC__/js/jquery.min.js"></script>
<script src="__PUBLIC__/js/bootstrap.min.js"></script>
<script src="__PUBLIC__/js/layui/layui.all.js"></script>
<script src="__PUBLIC__/dist/summernote.js"></script>
<script src="__PUBLIC__/dist/lang/summernote-zh-CN.js"></script>    <!-- 中文-->
<script>
	$('#post').on('click', function () {
		if ($('#summernote').summernote('isEmpty')) {
			layer.msg('内容不能为空');
		} else {
			var ajax_data = $('#summernote').summernote('code');
			$.ajax({
				data: {
					'data': ajax_data
				},
				type: 'POST',
				url: 'add_blog',
				dataType: 'JSON',
				success: function (data) {
					if (data.status == 'success') {
						layer.msg('发表成功');
						$('#summernote').summernote('code', '');
					} else {
						layer.msg('发表失败');
					}
				},
				error: function () {
					layer.msg('发表失败');
				}
			})
		}
	})
	$(document).ready(function () {
		$('#summernote').summernote({
			lang: 'zh-CN',
			placeholder: '请在此输入你的动态内容...',
			height: 200,
			callbacks: {
				onImageUpload: function (files, editor, editable) {
					sendFile(files[0], editor, editable);
				}
			}
		});
	}).on('click', function () {
		console.log($('#summernote').summernote('code'));
	})

	function sendFile(files, editor, editable) {
		var data1 = new FormData();
		data1.append('file', files);
		$.ajax({
			data: data1,
			type: 'POST',
			url: 'upload_image.html',
			dataType: 'json',
			contentType: false,
			processData: false,
			success: function (data) {
				$('#summernote').summernote('insertImage', data.dir, 'img');
			},
			error: function () {
				layer.msg('文件上传失败');
			}
		})
	}
</script>
</html>
